load("//bazel:yr.bzl", "COPTS", "LOPTS", "cc_strip")

genrule(
    name = "version",
    srcs = ["src/utils/version.h.in"],
    outs = ["src/utils/version.h"],
    cmd = """
            cat $(location :src/utils/version.h.in) > $@
            sed -i "s/@BUILD_VERSION@/$$BUILD_VERSION/g" $@
          """,
)

cc_library(
    name = "yr_api_lib",
    srcs = glob([
        "src/*.cpp",
        "src/*.h",
        "src/runtime/*.cpp",
        "src/runtime/*.h",
        "src/parallel_for/*.h",
        "src/parallel_for/*.cpp",
        "src/utils/*.h",
        "src/utils/*.cpp",
        "src/executor/*.h",
        "src/executor/*.cpp",
    ]) + ["src/utils/version.h"],
    hdrs = glob([
        "include/yr/*.h",
        "include/yr/api/*.h",
        "include/yr/parallel/*.h",
        "include/yr/parallel/detail/*.h",
    ]),
    copts = COPTS,
    linkopts = ["-lstdc++fs"],
    linkstatic = True,
    strip_include_prefix = "include",
    visibility = ["//visibility:public"],
    deps = [
        ":version",
        "//:runtime_lib",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/flags:parse",
        "@securec",
    ],
    alwayslink = True,
)

cc_binary(
    name = "libyr-api.so",
    copts = COPTS,
    dynamic_deps = ["//:grpc_dynamic"],
    linkopts = LOPTS,
    linkshared = True,
    linkstatic = True,
    visibility = ["//visibility:public"],
    deps = [":yr_api_lib"],
)

cc_library(
    name = "yr_cpp_lib",
    srcs = ["libyr-api.so"],
    hdrs = glob([
        "include/yr/*.h",
        "include/yr/api/*.h",
        "include/yr/parallel/*.h",
        "include/yr/parallel/detail/*.h",
    ]),
    strip_include_prefix = "include",
    visibility = ["//visibility:public"],
)

cc_binary(
    name = "runtime",
    srcs = [
        "src/main/yr_runtime_main.cpp",
    ],
    copts = COPTS,
    linkopts = LOPTS + ["-pie -lpthread"],
    linkstatic = True,
    deps = [
        "yr_cpp_lib",
        "@boost",
        "@msgpack",
    ],
)

cc_strip(
    name = "cpp_strip",
    srcs = [
        "libyr-api.so",
        "runtime",
    ],
)

filegroup(
    name = "cpp_include",
    srcs = glob([
        "include/**/*.h",
    ]),
)

genrule(
    name = "yr_cpp_pkg",
    srcs = [
        ":cpp_strip",
        "//:grpc_strip",
        "@boringssl//:gen_dir",
        ":cpp_include",
    ],
    outs = ["yr_cpp_pkg.out"],
    cmd = """
        BASE_DIR="$$(pwd)" &&
        CPP_SDK_DIR=$$BASE_DIR/build/output/runtime/sdk/cpp &&
        CPP_SERVICE_DIR=$$BASE_DIR/build/output/runtime/service/cpp &&
        rm -rf $$CPP_SDK_DIR $$CPP_SERVICE_DIR &&
        mkdir -p $$CPP_SDK_DIR/include $$CPP_SDK_DIR/include/boost $$CPP_SDK_DIR/lib $$CPP_SDK_DIR/bin $$CPP_SERVICE_DIR &&
        cp -rf $$BASE_DIR/external/msgpack/include $$CPP_SDK_DIR &&
        cp -rf $(locations @boringssl//:gen_dir)/include/openssl $$CPP_SDK_DIR/include &&
        cp -rf $$BASE_DIR/external/boost/boost/asio $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/any $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/assert $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/bind $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/callable_traits $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/chrono $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/config $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/container_hash $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/core $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/date_time $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/detail $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/exception $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/fiber $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/function $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/functional $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/fusion $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/integer $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/io $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/move $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/mpl $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/numeric $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/optional $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/predef $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/preprocessor $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/ratio $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/regex $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/smart_ptr $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/system $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/type_index $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/type_traits $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/utility $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/variant $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/winapi $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/*.hpp $$CPP_SDK_DIR/include/boost &&
        cp -rf $$BASE_DIR/external/boost/boost/*.h $$CPP_SDK_DIR/include/boost &&
        cp -rf $(locations @boringssl//:gen_dir)/lib/libssl.so.1.1 $$CPP_SDK_DIR/lib
        cp -rf $(locations @boringssl//:gen_dir)/lib/libcrypto.so.1.1 $$CPP_SDK_DIR/lib
        chmod +w $(locations :cpp_strip) $(locations //:grpc_strip) &&
        chrpath -d $(locations :cpp_strip) $(locations //:grpc_strip) &&
        cp -rf $(locations :cpp_strip) $(locations //:grpc_strip) $$CPP_SDK_DIR/lib/ &&
        cp -rf $(locations :cpp_strip) $$CPP_SDK_DIR/bin/ &&
        rm -rf $$CPP_SDK_DIR/bin/libyr-api.so
        cp -rf $$BASE_DIR/api/cpp/include $$CPP_SDK_DIR/ &&
        DATASYSTEM_DIR=$$BASE_DIR/external/datasystem_sdk/cpp &&
        cp -rf $$DATASYSTEM_DIR/include $$CPP_SDK_DIR &&
        cp -rf $$DATASYSTEM_DIR/lib $$CPP_SDK_DIR &&
        METRICS_DIR=$$BASE_DIR/external/metrics_sdk &&
        cp -rf $$METRICS_DIR/include/* $$CPP_SDK_DIR/include &&
        cp -rf $$METRICS_DIR/lib/* $$CPP_SDK_DIR/lib &&
        EXAMPLE_3RD_DIR=$$BASE_DIR/api/cpp/example/3rd &&
        rm -rf $$EXAMPLE_3RD_DIR &&
        mkdir $$EXAMPLE_3RD_DIR &&
        cp -rf $$CPP_SDK_DIR/include $$EXAMPLE_3RD_DIR &&
        cp -rf $$CPP_SDK_DIR/lib $$EXAMPLE_3RD_DIR &&
        cp -rf $$CPP_SDK_DIR/bin $$CPP_SERVICE_DIR &&
        cp -rf $$CPP_SDK_DIR/lib $$CPP_SERVICE_DIR
        echo "$$BASE_DIR" > $@
    """,
    local = True,
    visibility = ["//visibility:public"],
)
